package cn.gdsdxy.controller;

import cn.gdsdxy.pojo.Student;
import cn.gdsdxy.service.impl.StudentServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class StudentController {
    @Autowired
    private StudentServiceImpl studentServiceImpl;
    @GetMapping("/student")
    public String getAllStudent(Model model){
        List<Student> studentList=studentServiceImpl.getAllStudent();
        model.addAttribute("studentList",studentList);
        return "student";
    }
    @GetMapping("/student/{number}/{name}")
    public String getAllStundent1(@PathVariable("number") int number,@PathVariable("name") String name,Model model){
        List<Student> studentList=studentServiceImpl.getStudentByNumberAndName(number,name);
        model.addAttribute("studentList",studentList);
        return "student";
    }

    //@PostMapping("/addStudent")
    @RequestMapping(value = "/addStudent",method = RequestMethod.POST)
    @ResponseBody //表示将返回值直接显示在浏览器上
    public String addStudent(int number,String name,String sex){
        String msg;
        //Student student=new Student(4,"张三","男");
        Student student =new Student(number,name,sex);
        if (studentServiceImpl.addStudent(student)>0)
            msg="sucess";
        else
            msg="fail";
        return msg;
    }

    @RequestMapping(value = "/deleteStudent",method = RequestMethod.POST)
    @ResponseBody
    public String deleteStudent(int number){
        String a;
        if (studentServiceImpl.deleteStudent(number)>0)
            a="sucess";
        else
            a="fail";
        return a;
    }

    @RequestMapping(value = "/updataStudent",method = RequestMethod.POST)
    @ResponseBody //表示将返回值直接显示在浏览器上
    public String updataStudent(int number,String name,String sex){
        String b;
        //Student student=new Student(4,"张三","男");
        Student student =new Student(number,name,sex);
        if (studentServiceImpl.updataStudent(student)>0)
            b="sucess";
        else
            b="fail";
        return b;
    }
    @GetMapping("/addStudentPage")
    public String toAddStudentPage(){
        return "addStudent";
    }
    @GetMapping("/deleteStudentPage")
    public String toDeleteStudentPage(){
        return "deleteStudent";
    }
    @GetMapping("/updataStudentPage")
    public String toUpdataStudentPage(){
        return "updataStudent";
    }
}
